# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=libgdiplus
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.6.1
pkgrel=2
pkgdesc="An Open Source Implementation of the GDI+ API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('MPL' 'LGPL')
url="https://www.mono-project.com/"
depends=("${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-cairo"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/mono/${_realname}/archive/${pkgver}.tar.gz"
        "0001-libgdiplus-5.4-fix-shared-installation.patch"
        "0002-libgdiplus-5.4-fix-conflicting-types-error.patch"
        "0003-libgdiplus-5.4-fix-font-windows-error.patch"
        "0004-libgdiplus-5.4-disable-tests.patch")
sha256sums=('deff863023950b1d1de7e47e44fc31c8ba39cfc06334737261965f697b2ad312'
            '07ab03de3c86f18a9871794f6dd41de8852b42994e7845c220ba9ad8713f0e12'
            '0fc780dd34c01623a6910d6920d17549347b80351ed1baae05ad64f65c475dd4'
            '2835964746038b10f08f3530c450d7380c0b545b1a5c4884b8f513d7e6c0f809'
            '56131cce482fac95e4bbdb9dc6c4affac591c3da7d0d6d519cb34f4794b508f4')

prepare() {
  cd ${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-libgdiplus-5.4-fix-shared-installation.patch"
  patch -Np1 -i "${srcdir}/0002-libgdiplus-5.4-fix-conflicting-types-error.patch"
  patch -Np1 -i "${srcdir}/0003-libgdiplus-5.4-fix-font-windows-error.patch"
  patch -Np1 -i "${srcdir}/0004-libgdiplus-5.4-disable-tests.patch"
  autoreconf -fiv
}

build() {
  cd "${srcdir}"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --without-x11

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix .pc file
  sed -i "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
  ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libgdiplus.pc
}
